<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :is-show-before="false">
        <template slot="after">
          <el-button size="small" type="primary" @click="$router.push('/employees/import')">导入</el-button>
          <el-button size="small" type="primary" @click="exportExcel">导出</el-button>
          <el-button size="small" type="primary" icon="el-icon-plus" @click="addEmployeesDialogVisble=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin:10px 0">
        <!-- 表格 -->
        <el-table border :data="employeesList">
          <el-table-column label="序号" type="index" width="80" sortable />
          <el-table-column label="姓名" prop="username" sortable />
          <el-table-column label="头像" sortable>
            <!-- 作用域插槽 向父组件传递数值 row是我们在父组件接收到数据后给数据取的名字-->
            <template slot-scope="{row}">
              <img style="width:150px;height:100px" :src="row.staffPhoto" @click="onShowCode(row.staffPhoto)">
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatType" sortable />
          <el-table-column label="部门" prop="departmentName" sortable />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable>
            <template slot-scope="{row}">
              {{ row.timeOfEntry | formatTime }}
            </template>

          </el-table-column>
          <el-table-column label="账户状态" prop="enableState" sortable />
          <el-table-column label="操作" sortable fixed="right" width="280">
            <template slot-scope="{row}">
              <el-button type="text" size="small" @click="$router.push('/employees/detail/'+row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :total="total"
            :page-size="query.size"
            layout="prev, pager, next"
            @current-change="onCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增员工弹层 -->
    <add-employess :visible.sync="addEmployeesDialogVisble" @add-success="loadEmployeesList" />
    <!-- 显示二维码弹层 -->
    <el-dialog
      title="二维码"
      :visible.sync="qrCodeDialogVisible"
      width="30%"
    >
      <canvas ref="code" />
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeesList } from '@/api/employees'
import pageTools from '@/components/PageTools/index.vue'
import employees from '@/api/constant/employees'
import AddEmployess from './components/add-employess.vue'
import { export_json_to_excel } from '@/vendor/Export2Excel'
import QrCode from 'qrcode'
// import { hireType } from '@/api/constant/employees'
console.log(export_json_to_excel)
const hireType = employees.hireType
// {hireType} from
export default {
  name: 'EmployeesList',
  components: { pageTools, AddEmployess },
  data() {
    return {
      query: {
        page: 1,
        size: 5
      },
      employeesList: [],
      total: 0,
      addEmployeesDialogVisble: false,
      qrCodeDialogVisible: false
    }
  },
  created() {
    this.loadEmployeesList()
  },
  methods: {
    async loadEmployeesList() {
      const res = await getEmployeesList(this.query)
      console.log(res)
      this.employeesList = res.rows
      this.total = res.total
    },
    onCurrentChange(val) {
      this.query.page = val
      this.loadEmployeesList()
    },
    formatType(row, column, cellValue, index) {
      const findItem = hireType.find(item => item.id === cellValue)
      return findItem ? findItem.value : '聘用形式错误'
    },
    exportExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        const { rows } = await getEmployeesList({
          page: 1,
          size: this.total
        })
        const headers = {
          '姓名': 'username',
          '手机号': 'mobile',
          '入职日期': 'timeOfEntry',
          '聘用形式': 'formOfEmployment',
          '转正日期': 'correctionTime',
          '部门': 'departmentName',
          '工号': 'workNumber'
        }
        const header = Object.keys(headers) // ['姓名','手机号','入职日期','聘用形式','转正日期','部门','工号',]
        // console.log(rows) //[ {},{},{} ], [[],[]]
        const data = rows.map(item => {
          // item: {username,mobile,timeOfEntry,formOfEmployment,correctionTime,departmentName,workNumber}
          // item.username item.mobile
          return header.map(item2 => {
            if (item2 === '聘用形式') {
              const findItem = hireType.find(typeItem => typeItem.id === (item[headers[item2]] - 0))
              return findItem ? findItem.value : '未知'
            }
            return item[headers[item2]]
          })
        })
        excel.export_json_to_excel({
          // header: ['姓名', '手机号', '入职日期', '聘用形式', '转正日期', '工号', '部门'], // 表头 必填
          header,
          multiHeader: [['入职日期', '其他信息', '', '', '', '', '部门']],
          merge: ['A1:A2', 'B1:F1', 'G1:G2'],
          // data: [[13661668761, 222], [13919993232, 333]], // 具体数据 必填
          data,
          // filename: 'excel-list', // 非必填
          filename: '员工列表',
          // autoWidth: true, // 非必填
          autoWidth: true,
          bookType: 'xlsx' // 非必填
        })
      })
    },
    onShowCode(imgUrl) {
      // 因为是给头像增加二维码 修改了dom结构  数据变了 ，驱动视图是异步的
      if (!imgUrl) return this.$message.error('该用户未设置头像')
      this.qrCodeDialogVisible = true
      this.$nextTick(_ => {
        QrCode.toCanvas(this.$refs.code, imgUrl)
      })
    }
  }

}
</script>

<style>

</style>
